using Godot;
using Godot.Collections;
using System;

public partial class UiInventory : Ui
{
	private ItemSlot ITEM_SLOT;
	private Map _map;
	private Player _player;
	private CInventory _c_inventory;
	private CEquipment _c_equipment;
	private GridContainer _itemGrid;
	private GridContainer _equipItemGrid;
	private ItemSlot _itemSlot;
	private TextEdit _textEdit;
	private int current_category = 0;
	public string DropSource = null;
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		
		var Game = GetNode<Game>("/root/Game");
		_map = Game.get_map();
		_player = _map.GetNode<Player>("%Player");
		_c_inventory = _player.GetNode<CInventory>("%CInventory");
		_c_inventory.item_changed += AddItem;
		_c_equipment = _player.GetNode<CEquipment>("%CEquipment");
		_c_equipment.EquipItemChanged += AddEquipment;
	}
	public override void Initialize()
	{
		_equipItemGrid = GetNode<GridContainer>("%EquipGrid");
		var ITEM_SLOT = ResourceLoader.Load<PackedScene>("res://resources/items/scripts/item_slot.tscn");
		_textEdit = this.GetNode<TextEdit>("%TextEdit");
		var Game = GetNode<Game>("/root/Game");
		_map = Game.get_map();
		_player = _map.GetNode<Player>("%Player");
		_c_inventory = _player.GetNode<CInventory>("%CInventory");
		_c_inventory.item_changed += AddItem;
		_c_equipment.EquipItemChanged += AddEquipment;
		_c_equipment.Initialize();

		_itemGrid = GetNode<GridContainer>("%ItemGrid");
		foreach(var item in _itemGrid.GetChildren())
		{
			item.Free();
			//GD.Print("初始化背包，释放");
		}
		for(int i = 0; i < _c_inventory.item_count; i++)
		{
			ItemSlot w_item_slot = (ItemSlot)ITEM_SLOT.Instantiate();
			_itemGrid.AddChild(w_item_slot);
			w_item_slot.Initialize();
			w_item_slot.ItemSlotSelcted += OnItemSlotSelected;
			w_item_slot.DropToItem += OnDropToItem;
			//GD.Print("增加空白格");
		}
		var children = _equipItemGrid.GetChildren();
		foreach(var child in children)
		{
			EquipItemSlot slot = (EquipItemSlot)child;
			slot.Initialize();
			slot.DropToEquipment += OnDropToEquipment;
		}
		
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		var Game = GetNode<Game>("/root/Game");
		var UIControl = GetNode<UIControl>("/root/UIControl");
		if(this.Visible == true)
		{
		}
		
	}
	public void AddItem(Item item)
	{
		//GD.Print("测试，增加道具：",item.name);
		update_items_display();
	}
	public void AddEquipment(Item item)
	{
		
		update_equipments_display();
	}

	public Array<Item>	get_item_category()
	{
		var items = _c_inventory.items;
		Array<Item> filter_items = items;
		
		if(current_category ==0)
		{
			return items;
		}
		else
		{
			
			for(int i = 0; i<filter_items.Count; i++)
			{
				if(filter_items[i] ==null)
				{
					continue;
				}
				if(filter_items[i].type==(Item.TYPE)current_category)
				{
					continue;
				}
				
			}
		}
		filter_items.Resize(_c_inventory.item_count);
		return filter_items;
	}

	public void update_items_display()
	{
		var items = get_item_category();
		int index = 0;
		foreach(Item item in items)
		{
			ItemSlot slot = (ItemSlot)_itemGrid.GetChild(index);
			slot.item = item;
			index++;
		}
	}

	public void update_equipments_display()
	{
		var equipments = _c_equipment.items;
		int index = 0;
		foreach(Item item in equipments.Values)
		{
			EquipItemSlot slot = (EquipItemSlot)_equipItemGrid.GetChild(index);
			index++;
			if(item ==null)
			{ continue; }
			slot.item = item;
			
		}
	}

	public void OnItemSlotSelected(Item item)
	{
		if(item ==null)
		{
			return;
		}

		_textEdit.Text = item.description;
	}
	public void OnDropToItem(Item item)
	{
		//additem
		_c_inventory.add_item(item);
		//removeitem
	
		_c_equipment.remove_item(DropSource,item);
		DropSource = null;
	}

	public void OnDropToEquipment(string to ,Item item)
	{
		//additem
		_c_equipment.add_item(to ,item);
		//removeitem
		if(DropSource == "itemslot")
		{_c_inventory.remove_item(item);}
		else
		{
			_c_equipment.remove_item(DropSource,item);
		}
		DropSource = null;

	}

	
}
